//<script>
//////////////////
// Helper Stuff //
//////////////////

// used to find the Automation server name
function getDomDocumentPrefix() {
	if (getDomDocumentPrefix.prefix)
		return getDomDocumentPrefix.prefix;
	
	var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
	var o;
	for (var i = 0; i < prefixes.length; i++) {
		try {
			// try to create the objects
			o = new ActiveXObject(prefixes[i] + ".DomDocument");
			return getDomDocumentPrefix.prefix = prefixes[i];
		}
		catch (ex) {};
	}
	
	throw new Error("Could not find an installed XML parser");
}

function getXmlHttpPrefix() {
	if (getXmlHttpPrefix.prefix)
		return getXmlHttpPrefix.prefix;
	
	var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
	var o;
	for (var i = 0; i < prefixes.length; i++) {
		try {
			// try to create the objects
			o = new ActiveXObject(prefixes[i] + ".XmlHttp");
			return getXmlHttpPrefix.prefix = prefixes[i];
		}
		catch (ex) {};
	}
	
	throw new Error("Could not find an installed XML parser");
}

//////////////////////////
// Start the Real stuff //
//////////////////////////


// XmlHttp factory
function XmlHttp() {}

XmlHttp.create = function () {
	try {
		if (window.XMLHttpRequest) {
			var req = new XMLHttpRequest();
			
			// some versions of Moz do not support the readyState property
			// and the onreadystate event so we patch it!
			if (req.readyState == null) {
				req.readyState = 1;
				req.addEventListener("load", function () {
					req.readyState = 4;
					if (typeof req.onreadystatechange == "function")
						req.onreadystatechange();
				}, false);
			}
			
			return req;
		}
		if (window.ActiveXObject) {
			return new ActiveXObject(getXmlHttpPrefix() + ".XmlHttp");
		}
	}
	catch (ex) { alert ("Your browser does not support XmlHttp objects") }
	// fell through
	throw new Error("Your browser does not support XmlHttp objects");
};

// XmlDocument factory
function XmlDocument() {}

XmlDocument.create = function () {
	try {
		// DOM2
		if (document.implementation && document.implementation.createDocument) {
			var doc = document.implementation.createDocument("", "", null);
			
			// some versions of Moz do not support the readyState property
			// and the onreadystate event so we patch it!
			if (doc.readyState == null) {
				doc.readyState = 1;
				doc.addEventListener("load", function () {
					doc.readyState = 4;
					if (typeof doc.onreadystatechange == "function")
						doc.onreadystatechange();
				}, false);
			}
		 
			doc.selectNodes      = selectXMLNodes;
			doc.selectSingleNode = selectXMLSingleNode;
			return doc;
		}
		if (window.ActiveXObject)
			return new ActiveXObject(getDomDocumentPrefix() + ".DomDocument");
	}
	catch (ex) { alert ("Your browser does not support XmlDocument objects") }
	throw new Error("Your browser does not support XmlDocument objects");
};

function selectXMLNodes(pred) {
   predicate = pred;
   if (predicate.indexOf("//")==0) {
       predicate = pred.substr(2);
   }
   
   return this.getElementsByTagName (predicate);
}

function selectXMLSingleNode(pred) {

   predicate = pred;
   if (predicate.indexOf("//")==0) {
       predicate = pred.substr(2);
   }
   caratL = predicate.indexOf("[")
   caratR = predicate.indexOf("]")
   
   if (caratL>0) {
       fact = predicate.substr(caratL+1,(caratR-caratL)-1)
       arr   = fact.split ("=");
       pname = arr[0];
       ptext = arr[1];
       if (pname.indexOf("@")==0) pname = pname.substr(1);
       if (ptext.indexOf("'")==0) {
           ptext = ptext.substr(1);
           ptext = ptext.substr(0,ptext.length-1);
       }
       tags = this.getElementsByTagName (predicate.substr(0,caratL))
       tag  = null;
      
       for (e=0;e<tags.length;e++) {
           if (tags[e].getAttribute (pname)==ptext) tag = tags[e] 
       }
       
       
       return tag
   } else return this.getElementsByTagName (predicate)[0];
   
   
}


function selectXMLChildNodes (pred, noisy) {
    return selectChildNodes (this, pred, noisy)
}


function selectChildNodes (element, pred, noisy) {

   var recurse = false, predicate = pred;
   if (predicate.indexOf("//")==0) {
       predicate = pred.substr(2);
       recurse   = true;
   }
   
   var result = new Array();
   var caratL = predicate.indexOf("[")
   var caratR = predicate.indexOf("]")   
   var tags   = element.childNodes 
   
   
   if (tags) {
        if (caratL>0) {            
          if (noisy)  alert ("2. predicate="+predicate)
            var fact = predicate.substr(caratL+1,(caratR-caratL)-1)
          if (noisy)   alert ("2. fact="+fact)
            var arr   = fact.split ("=");
            var pname = arr[0];
            var ptext = arr[1];
            if (pname.indexOf("@")==0) pname = pname.substr(1);
            if (ptext.indexOf("'")==0) {
                ptext = ptext.substr(1);
                ptext = ptext.substr(0,ptext.length-1);
            } 
            if (noisy) alert ("2. pname="+pname)
            if (noisy) alert ("2. ptext="+ptext)
            for (var e=0;e<tags.length;e++) {
                if (tags[e].tagName==predicate.substr(0,caratL)&&tags[e].getAttribute (pname)==ptext) {
                    result[result.length] = tags[e] 
                }
            }  
        } else {
            if (noisy)  alert ("1. predicate="+predicate+" "+tags.length)
            for (var e=0;e<tags.length;e++) {
                if (noisy)  alert ("predicate="+predicate+", tags["+e+"].tagName="+tags[e].tagName)
                if (tags[e].tagName==predicate) {
                    result[result.length] = tags[e] 
                }
            } 
            if (noisy)  alert ("1. predicate="+predicate+" "+result.length)
 
        } 
   } 
    if (result.length==0) {
        while (e--) {
            if (tags[e].childNodes.length>0) {
                result = selectChildNodes (tags[e], pred, noisy);
            }
            if (result.length>0) break;
        }
    }   
   return result;
}

function _Document_load(strURL) {
 alert (strURL)
    //change the readyState
    this.readyState = 1;
    
    //call the original load method
    this.__load__(strURL);
}



// Create the loadXML method and xml getter for Mozilla
if (window.DOMParser &&
	window.XMLSerializer &&
	window.Node && Node.prototype && Node.prototype.__defineGetter__) {

	// XMLDocument did not extend the Document interface in some versions
	// of Mozilla. Extend both!
	//XMLDocument.prototype.loadXML = 
	
	Document.prototype.loadXML = function (s) {
		
		// parse the string to a new doc	
		var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
		
		// remove all initial children
		while (this.hasChildNodes())
			this.removeChild(this.lastChild);
			
		// insert and import nodes
		for (var i = 0; i < doc2.childNodes.length; i++) {
			this.appendChild(this.importNode(doc2.childNodes[i], true));
		}
		
		return true;
	};
	
//	 Document.prototype.__load__ = Document.prototype.load; 
     Document.prototype.load = _Document_load;

	
	/*
	 * xml getter
	 *
	 * This serializes the DOM tree to an XML String
	 *
	 * Usage: var sXml = oNode.xml
	 *
	 */
	// XMLDocument did not extend the Document interface in some versions
	// of Mozilla. Extend both!
	/*
	XMLDocument.prototype.__defineGetter__("xml", function () {
		return (new XMLSerializer()).serializeToString(this);
	});
	*/
	Document.prototype.__defineGetter__("xml", function () {
		return (new XMLSerializer()).serializeToString(this);
	});
}